import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { MyError } from './Error';
import ErrorExcption from './ErrorExcption';

@Catch()
export class AllExceptionFilter implements ExceptionFilter {
  catch(exception: ErrorExcption, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();
    console.log(exception);
    const errmessage = exception.getResponse() as MyError;

    // const status =
    //   exception instanceof HttpException
    //     ? exception.getStatus()
    //     : HttpStatus.INTERNAL_SERVER_ERROR;

    response.status(200).json({
      statusCode: errmessage.statusCode,
      errMsg: JSON.stringify(errmessage.message) || 'Internal Error',
      timestamp: new Date(),
      path: request.url,
    });
  }
}
